import { get, post } from '../http'
import { domainid } from '@/utils/cloud/utils'

// 获取设备分组标签
export function getDeviceTags(params, domain_id) {
  return get(`/operation/${domain_id || domainid()}/tag`, params)
}

export function getDevice(params) {
  return get(`/operation/${domainid()}/device`, params)
}

// 查询网关
export function getGateway(params, domain_id) {
  return get(`/operation/${domain_id || domainid()}/gateway`, params)
}

// 查询计量设备
export function getEnergyMeter(params) {
  return get(`/operation/${domainid()}/energy_meter`, params)
}

// 查询控制器
export function getController(params) {
  return get(`/operation/${domainid()}/controller`, params)
}

// 获取用电类型字典
export function getEnergyTypeDict(params) {
  return get(`/operation/${domainid()}/energy/energy_type_dict`, params)
}

// 获取sql数据
export function getSqlData(params, format = 'dict') {
  let ems = 'linku_ems'

  return get(`/tsdb/${ems}?format=${format}`, params)
}

export function postSqlData(params) {
  let ems = 'linku_ems'

  return post(`/tsdb/${ems}?format=dict`, params)
}

// 获取产品类别字典
export function getCategoryDict(params) {
  return get('/thing_model/category/list', params)
}

// 获取时序数据
export function getDatapoolTsdb(params, name) {
  let data = {
    name,
    application: 'linku_ems',
    datasource: 'ems_tsdb',
    param: {
      domain_id: domainid(),
      ...params
    }
  }
  return post(`/datapool/tsdb?format=dict`, data)
}

// 获取设备最后告警状态
export function getAlarmLatestState(params) {
  return get(`/operation/${domainid()}/alert/latest`, params)
}

// 获取存在active_power的设备
export function getPowerDevice(params) {
  return get(`/operation/${domainid()}/product/list/power_device`, params)
}

//字典
export function getDictionary(param, type) {
  //参数iot_id,还有设备类型
  let url = ''
  if (type == 'iot_id') {
    if (!param) return
    url = `/thing_model/device/${param}/property_dict`
  } else if (type === 'category') {
    url = `/thing_model/category/${param}/property_dict`
  } else if (type === 'productkey') {
    url = `/thing_model/product/${param}/property_dict`
  }
  return get(url)
}

// 新建或修改设备
export function createOrUpdateData(params) {
  const { tag_id, tag_dev_id, data } = params
  let url = `/operation/${domainid()}/batch`
  if (tag_dev_id) {
    url += `?tag_dev_id=${tag_dev_id}`
    if (tag_id) {
      url += `&tag_id=${tag_id}`
    }
  } else if (tag_id) {
    url += `?tag_id=${tag_id}`
  }
  return post(url, data)
}

// 上报日志
export function submitOperationLog({ type, data }) {
  return post(`operation/${domainid()}/controller/${type}/${data.iot_id}/parameter`, data)
}

// 获取channel的历史数据
export function getDeviceChannelTimeData(data) {
  let url = import.meta.env.VITE_CLOUD_BASE_URL || import.meta.env.VITE_CLOUD_PROXY_URL
  let ems = 'linku_ems_prod'
  if (url.includes('servicedev')) {
    ems = 'linku_ems'
  }
  return get(`/tsdb/${ems}?format=dict`, {
    sql: `select time, ${data.channel} from ${data.table} where domain_id='${domainid()}' and iot_id='${
      data.iot_id
    }' and time >= '${data.startDateTime}' and time <= '${data.endDateTime}' order by time ${data.order || 'asc'}`
  })
}

// 获取告警明细
export function getAlarmListAPI(params) {
  return get(`/operation/${domainid()}/alert/list`, params)
}

// 获取操作日志
export function getOperationLogAPI(params) {
  let data = {
    application: 'linku_ems',
    datasource: 'ems_backend',
    name: 'operation_log',
    param: {
      ...params,
      domain_id: domainid()
      // domain_id: 'ee5ed02981484438b457bcf0f299a139'
    }
  }
  return post(`/datapool/mysql?format=row`, data)
}

// 设置设备控制channel
export function setDeviceControl(data) {
  return post(`/iot_dev/control`, data)
}
